<?php
/**
 * 熵巡 - 企业级氚云OpenAPI数据管理平台
 * 开发归属 Alyr.net
 * 氚云API调用测试 - 测试连接、认证、数据接口
 * 尊重劳动成果是一种美德
 */

// 禁用错误输出防止HTML污染
error_reporting(0);
ini_set('display_errors', 0);

// 设置session目录权限
ini_set('session.save_path', sys_get_temp_dir());
session_start();

// 检查登录状态
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
    header('Content-Type: application/json');
    echo json_encode(['success' => false, 'message' => '请先登录']);
    exit;
}

// 引入配置文件和客户端类
require_once '../config.php';
require_once '../classes/H3OpenApiClient.php';

header('Content-Type: application/json');

try {
    $config = require '../config.php';
    $client = new H3OpenApiClient($config);
    
    $results = [
        'connection' => false,
        'auth' => false, 
        'api' => false,
        'connection_msg' => '',
        'auth_msg' => '',
        'api_msg' => '',
        'http_code' => 0
    ];
    
    // 1. 测试连接
    try {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $config['baseUrl']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_NOBODY, true);
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        
        $results['http_code'] = $httpCode;
        $results['connection'] = ($httpCode >= 200 && $httpCode < 400);
        $results['connection_msg'] = $results['connection'] 
            ? '连接成功' 
            : '连接失败';
    } catch (Exception $e) {
        $results['connection'] = false;
        $results['connection_msg'] = '连接异常: ' . $e->getMessage();
    }
    
    // 2. 测试认证（只有连接成功才测试）
    if ($results['connection']) {
        try {
            $testResult = $client->loadBizObjects($config['schema_code'], [
                'FromRowNum' => 0,
                'ToRowNum' => 1
            ]);
            
            $results['auth'] = is_array($testResult);
            $results['auth_msg'] = $results['auth'] ? '认证成功' : '认证失败';
        } catch (Exception $e) {
            $results['auth'] = false;
            $results['auth_msg'] = '认证失败: ' . $e->getMessage();
        }
    } else {
        $results['auth_msg'] = '跳过测试（连接失败）';
    }
    
    // 3. 测试API调用（只有认证成功才测试）
    if ($results['auth']) {
        try {
            $apiResult = $client->loadBizObjects($config['schema_code'], [
                'FromRowNum' => 0,
                'ToRowNum' => 10
            ]);
            
            $results['api'] = is_array($apiResult);
            $results['api_msg'] = $results['api'] ? 'API调用成功' : 'API调用失败';
        } catch (Exception $e) {
            $results['api'] = false;
            $results['api_msg'] = 'API调用失败: ' . $e->getMessage();
        }
    } else {
        $results['api_msg'] = '跳过测试（认证失败）';
    }
    
    echo json_encode($results);
    
} catch (Exception $e) {
    echo json_encode([
        'connection' => false,
        'auth' => false,
        'api' => false,
        'connection_msg' => '系统错误: ' . $e->getMessage(),
        'auth_msg' => '未执行',
        'api_msg' => '未执行'
    ]);
}
?>